package com.sea.finance.controller;

import com.codecrane.core.util.DateUtils;
import com.codecrane.core.util.ExcelUtil;
import com.sea.supply.entity.BillRefused;
import com.sea.supply.entity.Buy;
import com.sea.supply.service.BillRefusedService;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping(value = "/sea/finance/report")
public class FinanceReportController {
    @Autowired
    private BillRefusedService billRefusedService;
    /**
     * 管理主页面
     * @param model
     * @return
     */
    @RequestMapping("/main")
    public String main(Model model) {

        return "/sea/finance/report/payment/main";
    }

    @RequestMapping(value = "/export", method = RequestMethod.GET)
    @ResponseBody
    public void export(HttpServletResponse response, BillRefused billRefused) {
        List<BillRefused> list = billRefusedService.findByCnd(billRefused);
        String[] title = {"海关单编号","柜号","渠道", "收货金额","发票金额","财务审批金额","财务付款金额","核销金额","杂项费","备注"};
        String fileName = "付款报表" + DateUtils.dateToString(new Date(), "yyyyMMdd") + ".xls";
        String sheetName = "付款报表";
        String[][] content = new String[list.size()][10];
        for (int i = 0; i < list.size(); i++) {
            content[i] = new String[title.length];
            BillRefused obj = list.get(i);
            content[i][0] = obj.getRefusedNo();
            content[i][1] = obj.getRefusedPlanNo();
            content[i][2] = obj.getRefusedChannel();
            content[i][3] = (null==obj.getRefusedCreateMoney()?0.00+"":obj.getRefusedCreateMoney()+"");
            content[i][4] = (null==obj.getRefusedBillMoney()?0.00+"":obj.getRefusedBillMoney()+"");
            content[i][5] = (null==obj.getRefusedFinanceMoney()?0.00+"":obj.getRefusedFinanceMoney()+"");
            content[i][6] = (null==obj.getRefusedPaymentMoney()?0.00+"":obj.getRefusedPaymentMoney()+"");
            content[i][7] = (null==obj.getProduceCheckMoney()?0.00+"":obj.getProduceCheckMoney()+"");
            content[i][8] = (null==obj.getProduceUnusualMoney()?0.00+"":obj.getProduceUnusualMoney()+"");
            content[i][9] = obj.getRefusedRemark();
        }
        //创建HSSFWorkbook
        HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(sheetName, title, content, null);
        //响应到客户端
        try {
            this.setResponseHeader(response, fileName);
            OutputStream os = response.getOutputStream();
            wb.write(os);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //发送响应流方法
    public void setResponseHeader(HttpServletResponse response, String fileName) {
        try {
            try {
                fileName = new String(fileName.getBytes(), "ISO8859-1");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            response.setContentType("application/octet-stream;charset=ISO8859-1");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }



}
